home *** CD-ROM | disk | FTP | other *** search
/ Technotools / Technotools (Chestnut CD-ROM)(1993).ISO / lang_c / ctags3a / tags.cb < prev   
Lisp/Scheme  |  1990-05-14  |  6KB  |  187 lines

  1. /*   File:          ctags.m              8/24/85                                             */
  2. /*   Writen by:  Yellow Pig XVII                                                          */
  3. /*                                                                                                  */
  4. /*   BRIEF macros to do vi-like tag.  Assumes the existence of a file         */
  5. /*   called "tags" which can be created using the "ctags" utility.  If         */
  6. /*   this file doesn't exist, tag is simply reported as "Not found."          */
  7. /*   The format of each line of the tags file must be:                             */
  8. /*                                                                                                  */
  9. /*   tagword  filename    ?^pattern$?                                                      */
  10. /*                                                                                                  */
  11. /*   This is the normal output produced by the Unix utility "ctags".          */
  12. /*                                                                                                  */
  13. /*   For those unfamiliar with tagging, it is really useful.  If you          */
  14. /*   create a tags file for all your C source files you can locate the         */
  15. /*   file and line where a given function is declared when you're in          */
  16. /*   the editor, even if that function is declared in a different              */
  17. /*   source file from the one you are editing.    Tagging will move you         */
  18. /*   to that line in that file.                                                             */
  19. /*                                                                                                  */
  20. /*   To use these BRIEF macros, you can call "tag" directly as a                 */
  21. /*   command.    It will ask you to enter a tag.    Give the name of the          */
  22. /*   function you are searching for (case sensitivity will depend on          */
  23. /*   the current setting in your editing session).  Or you can assign         */
  24. /*   the macro "ctag" to a key.    If you do this, you can place the             */
  25. /*   cursor anywhere on the name of a function you wish to tag to              */
  26. /*   (presumably an occurrence of it where it is being called, but             */
  27. /*   this is not necessary -- you can tag to a function if you are             */
  28. /*   already there!!) and hit the key you assigned.                                 */
  29. /*                                                                                                  */
  30.  
  31. /*   ctag                                                                                         */
  32. /*                                                                                                  */
  33. /*   Checks to make sure cursor is on a valid function name.  A valid         */
  34. /*   function name is considered to be any string of alphanumeric              */
  35. /*   characters (including the underscore) which begins with a letter.         */
  36. /*                                                                                                  */
  37. /*   If the tag is invalid, the cursor position remains the same.              */
  38. /*   Otherwise the full word is read and passed to "tag".                         */
  39. /*
  40. ** Changes/Additions by Michael Denio 5/15/90
  41. ---------------------------------------------
  42.     The program now looks for all CTAGS.TAG files specified in directories
  43.     in the CTAGS environment variable.  It first looks in the current
  44.     directory. Example:  set CTAGS=C:\;N:\LIB\SRC;
  45.  
  46.     Place the following in your initials file:
  47.     (autoload "tags" "ctag" "tag" "read_word")
  48.  
  49.     You can then assign a key to the ctag macro.
  50.  
  51. **/
  52.  
  53. /* Environment variables that hold the location of MAINTAGS & MYTAGS */
  54. #define TAGENV  "CTAGS"         // CTAGS environment variable
  55. #define TAGFILE "CTAGS.TAG"     // CTAGS filename
  56.  
  57. ctag (...)
  58. {
  59.     string    tagstr, badchar;
  60.  
  61.     int    cch, row, col, col2;
  62.  
  63.     inq_position (row, col);
  64.     if (col != 1)
  65.         {
  66.         search_back ("[~A-Za-z0-9_]");
  67.         inq_position (NULL, col2);
  68.         if (col != col2)
  69.             next_char ();
  70.         }
  71.  
  72.     if (0 == index ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", badchar = read (1)))
  73.         {
  74.         message ("Invalid tag: %s", badchar);
  75.         move_abs (row, col);
  76.         }
  77.     else
  78.         {
  79.         read_word (0, cch, tagstr);
  80.         tag (tagstr);
  81.         }
  82. }
  83.  
  84. /*   tag                                                                                          */
  85. /*                                                                                                  */
  86. /*   Looks in tags file for given tag, gets the filename and search             */
  87. /*   pattern to use in locating the tag, edits the given file and              */
  88. /*   searches for the pattern.  If the tag isn't found, reports an             */
  89. /*   error.  There is no error checking in this macro for a valid              */
  90. /*   function name (tag).    This macro will most often be used in                 */
  91. /*   conjunction with the "ctag" macro which does this error checking.         */
  92.  
  93. tag (...)
  94. {
  95.     string    tagstr, file, pattern, tag_path, tag_file;
  96.  
  97.     int    cch, buf, tagbuf, semi_spot;
  98.  
  99.     buf = inq_buffer ();
  100.     tagbuf = create_buffer ("tags", NULL, 0);
  101.     set_buffer (tagbuf);
  102.  
  103.     // If one exists read the CTAGS.TAG file from the current directory
  104.     if (exist("CTAGS.TAG"))
  105.         read_file("CTAGS.TAG");
  106.  
  107.     // Search for all CTAGS.TAG file in the CTAGS environment variable
  108.     tag_path = inq_environment ("CTAGS");
  109.     if (substr(tag_path, strlen(tag_path), 1) != ";")
  110.         tag_path = tag_path + ";";
  111.     if (tag_path != "")
  112.     {
  113.         while (semi_spot = index (tag_path, ";"))
  114.         {
  115.             tag_file = substr(tag_path, 1, semi_spot - 1);
  116.             if (substr(tag_file, strlen(tag_file), 1) != "\\")
  117.                 tag_file = tag_file + "\\";
  118.             tag_file = tag_file + TAGFILE;
  119.             tag_path = substr(tag_path, semi_spot + 1);
  120.             read_file(tag_file);
  121.         }
  122.     }
  123.  
  124.     get_parm (0, tagstr, "Enter tag: ");
  125.     message ("Tagging: %s ...", tagstr);
  126.     move_abs (1, 1);
  127.     if (search_fwd ("%" + tagstr) > 0)
  128.         {
  129.         search_fwd ("[\t ]");
  130.         search_fwd ("[~\t ]");
  131.         read_word (1, cch, file);
  132.         search_fwd ("[\t ]");
  133.         search_fwd ("[~\t ]");
  134.         pattern = read ();
  135.         pattern = "%" + substr (pattern, 3, strlen (pattern) - 4);
  136.         edit_file (file);
  137.         end_of_buffer ();
  138.         search_back (pattern);
  139.         message ("");
  140.         }
  141.     else
  142.         {
  143.         message ("Tag not found: %s", tagstr);
  144.         set_buffer (buf);
  145.         }
  146.     if (tagbuf != buf)
  147.         delete_buffer (tagbuf);
  148. }
  149.  
  150. /*   read_word <srch_code> <cch> <word>                                                 */
  151. /*                                                                                                  */
  152. /*   Reads one alphanumeric word starting at current position.                  */
  153. /*   Returns length of word and the word itself.  Srch_code is                  */
  154. /*   a code that determines what type of "word" to search for.                  */
  155. /*   0 - alphanumeric word                                                                  */
  156. /*   1 - non whitespace word                                                                 */
  157. /*                                                                                                  */
  158.  
  159. read_word (...)
  160. {
  161.     int    cch, row, col, srch_code;
  162.  
  163.     string    alphanum, char, word;
  164.  
  165.     alphanum = "_ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";
  166.     get_parm (0, srch_code);
  167.     inq_position (row, col);
  168.     cch = 0;
  169.     if (srch_code == 1)
  170.         while (!index ("\n\t ", char = read (1)))
  171.             {
  172.             ++cch;
  173.             right ();
  174.             }
  175.     else
  176.         while (index (alphanum, char = read (1)))
  177.             {
  178.             ++cch;
  179.             right ();
  180.             }
  181.  
  182.     move_abs (row, col);
  183.     word = read (cch);
  184.     put_parm (1, cch);
  185.     put_parm (2, word);
  186. }
  187.